/**********************************************************************************************/
/* FBL_Starter_Unix.cpp                                                						  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2006															  */
/* All Rights Reserved                                                   					  */
/**********************************************************************************************/

#include <VShared/FBL/publ/Headers/StdAfx.h>

#include <VShared/FBL/publ/Starter/FBL_Starter.h>

#include <VShared/FBL/publ/Interfaces/FBL_I_Disk_Location.h>
#include <VShared/FBL/publ/File_OS/FBL_DynamicLibrary.h>
#include <VShared/FBL/publ/Util_classes/FBL_String.h>


/**********************************************************************************************/
#if FBL_UNIX


/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
extern "C" void InitBackpointers( void );


/**********************************************************************************************/
void Init_ValentinaDlls( bool inUseClientStrings )
{
#if FBL_STATIC

	argused1( inUseClientStrings );
	InitBackpointers();

#else

	// Kernel-dll string-factory has priority.
	if( String::sFactory && inUseClientStrings )
		return;

	UnixHandle hDLL;
	
	DynamicLibrary_Ptr lib = new DynamicLibrary();

	// Try to load local implementation.
	if( inUseClientStrings || (hDLL = ::dlopen(GetKernelDllName(), RTLD_NOW)) == NULL )
	{
		// Try to load client implementation.
		if( !(hDLL = ::dlopen(GetClientDllName(), RTLD_NOW)) )
		{
			// CRITICAL SITUATION: neither kernel nor client dll was not found!
			// Valentina cannot continue to work.
			// FIXME: throw exception!
			int a = 2;
			a += 1;

			return;
		}
	}

	// --------------------------------
	// Get init callbacks function.
	typedef void (*InitBackpointers_PTR)( void );
	InitBackpointers_PTR pfn;

	// Store reference to the function and call it:
	pfn = (InitBackpointers_PTR) ::dlsym( hDLL, "InitBackpointers" );
	pfn();

#endif // FBL_STATIC
}


/**********************************************************************************************/
FBL_End_Namespace

#endif // FBL_UNIX
